let layer = layui.layer;
var form = layui.form;

// 1、初始化 获取用户信息
editUser();
// 保存id
let hx_id;
function editUser() {
  axios({
    method: 'GET',
    url: 'http://124.223.14.236:8060/home/user/getUserInfo',
    headers: {
      token: localStorage.getItem('token'),
    },
  }).then(function (res) {
    // console.log(res);
    if (res.status !== 200) {
      return layer.msg('获取用户信息失败！');
    }
    // console.log(123);
    var data = res.data.data.userInfo;
    hx_id = data.id;
    // data.sex==0?'男':'女'
    form.val('formUserInfo', data);
  });
}

// 2、点击图片 自动点击文档
$('#chooseImage').on('click', function () {
  $('#files').click();
});

// 3、上传图片
// 保存路径
let hx_URL;
$('#files').on('change', function (e) {
  let fd = new FormData($('#form2')[0]);
  $.ajax({
    url: 'http://124.223.14.236:8060/common/upload?type=images',
    method: 'POST',
    data: fd,
    headers: {
      token: localStorage.getItem('token'),
    },
    // 注意：formData格式的请求要加上下面两项配置
    contentType: false,
    processData: false,
    success: function (res) {
      //   console.log(res)
      hx_URL = 'http://124.223.14.236:8060/' + res.data.savePath;

      console.log(hx_URL);
      $('#chooseImage').prop('src', hx_URL);
    },
  });
});

// 4、点击取消
$('.btn-warning').on('click', function (e) {
  e.preventDefault();
  editUser();
});

// 5、点击立即修改
// console.log($('.layui-form'));
$('.btn-success').on('click', function (e) {
  e.preventDefault();
  // 传入id  上传图片地址URL
  var data = {
    username: $('#form5 [name=username]').val().trim(),
    mobile: $('#form5 [name=mobile]').val().trim(),
    icon: hx_URL,
    address: $('#form5 [name=address]').val().trim(),
    notes: $('#form5 [name=notes]').val().trim(),
    sex: 0,
    birthday: $('#form5 [name=birthday]').val().trim(),
    id: hx_id,
  };
  console.log(data);
  // console.log(data);
  axios({
    method: 'POST',
    url: 'http://124.223.14.236:8060/home/user/update',
    headers: {
      token: localStorage.getItem('token'),
    },
    data: data,
  }).then(function (res) {
    console.log(res);
    if (res.status !== 200) {
      return;
    }
    if (res.data.errno !== 0) return layer.msg('修改信息失败');
    layer.msg('修改信息成功');
    location.href = './user.html';
  });
  console.log(123);
});
// 首页和全局数据
function homePage() {
  axios({
    method: 'GET',
    url: 'http://124.223.14.236:8060/home/index/index',
  }).then(function (res) {
    // console.log(res);
    // 导航栏
    let strNav = res.data.data.allCate
      .map(item => {
        return `
          <li class="menu-item menu-item-type-taxonomy menu-item-object-category">
              <a href="list.html" id=${item.id}><span class="fa fa-bullhorn iconfont ${item.icon}"></span>${item.catename}</a>
          </li>
          `;
      })
      .join('');
    $('#nav').append(strNav);
  });
}
homePage();
userCenter();
// 用户中心和退出
function userCenter() {
  // 没有token的时候隐藏用户中心和退出
  if (localStorage.getItem('token')) {
    let str = `
         <a href="user.html" id="user"><i class="fa fa-user"></i>&nbsp;用户中心</a>
         <a href="javascript:;" class="quit"><i></i>&nbsp;退出</a>`;
    $('.slinks').html(str);
  } else {
    let str = `
          <a href="login.html"><i class="fa fa-hand-o-right"></i>&nbsp;登录</a> |
          <a href="reg.html"><i class="fa fa-registered"></i>&nbsp;注册</a>`;
    $('.slinks').html(str);
  }
  $('#user').on('click', function (e) {
    e.preventDefault();
    if (localStorage.getItem('token')) {
      location.href = 'user.html';
    } else {
      location.href = 'login.html';
    }
  });
  $('.quit').on('click', function (e) {
    layer.confirm('你确定要退出吗?', { icon: 3, title: '退出提示' }, function (index) {
      localStorage.removeItem('token');
      location.reload();

      layer.close(index);
    });
  });
}
// 导航栏跳转
function getNavSkip() {
  $('#nav').on('click', 'a', function (e) {
    e.preventDefault();
    window.open('../list.html?id=' + $(this).attr('id'));
  });
}
getNavSkip();
